#include "boa_interface.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <arpa/inet.h>

Onvif_Config_Data onvifConfig;

int netGetMac(char *pInterface, unsigned char *pMac)
{
	struct ifreq ifreq;
	int sockfd = 0;
	unsigned char mac[6] = {0}; 

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("netGetMac socket");
		return -1;
	}

	strcpy(ifreq.ifr_name, pInterface);

	if(ioctl(sockfd, SIOCGIFHWADDR, &ifreq) < 0)
	{
		perror("netGetMac ioctl");
		close(sockfd);
		return -2;
	}

	memcpy(mac, ifreq.ifr_hwaddr.sa_data, 6);
	printf("MAC:%02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

	if(pMac != NULL)
	{
		memcpy(pMac, mac, 6);
	}

	close(sockfd);

	return 0;
}


int netGetIp(char *pInterface, unsigned int *ip)
{
	int sock = 0;
	struct ifreq ifr;

	if((pInterface == NULL) || (*pInterface == '\0'))
	{
		printf("get ip: pInterface == NULL\r\n");
		return -1;
	}

	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, pInterface, IFNAMSIZ);
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if(sock <= 0)
	{
		printf("get ip: sock error, %s\r\n", strerror(errno));
		return -1;
	}

	((struct sockaddr_in*)&ifr.ifr_addr)->sin_family = PF_INET;
	if(ioctl(sock, SIOCGIFADDR, &ifr) < 0)
	{
		printf("get ip error: %s\r\n", strerror(errno));
		close(sock);
		return -1;
	}
	else
	{
		*ip = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr.s_addr;
		printf("get ip(%d:%d:%d:%d) success!\r\n", (*ip)&0xff, (*ip>>8)&0xff, (*ip>>16)&0xff, (*ip>>24)&0xff);
	}
	close(sock);

	return 0;
}


in_addr_t net_get_ifaddr(char *ifname)
{
	struct ifreq ifr;
	int skfd;
	struct sockaddr_in *saddr;

	if ( (skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
		//DBG_ERR("socket error");
		return -1;
	}

	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
	if (ioctl(skfd, SIOCGIFADDR, &ifr) < 0) {
		//DBG_ERR("net_get_ifaddr: ioctl SIOCGIFADDR");
		close(skfd);
		return -1;
	}
	close(skfd);

	saddr = (struct sockaddr_in *) &ifr.ifr_addr;
	return saddr->sin_addr.s_addr;

	return 0;
}

int net_get_hwaddr(char *ifname, unsigned char *mac)
{
	struct ifreq ifr;
	int skfd;

	if ( (skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
		//DBG_ERR("socket error");
		return -1;
	}

	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
	if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) {
		//DBG_ERR("net_get_hwaddr: ioctl SIOCGIFHWADDR");
		close(skfd);
		return -1;
	}
	close(skfd);

	memcpy(mac, ifr.ifr_ifru.ifru_hwaddr.sa_data, IFHWADDRLEN);
	return 0;
}

void OnvifParamInit(Onvif_Config_Data *onvifParam)
{
	int i;
	char tmpBuf[VIDEO_NAME_LEN];

	if(onvifParam == NULL)
		return;

	memset(onvifParam,0,sizeof(Onvif_Config_Data));
	onvifParam->nProfile = 1;
	for(i = 0; i < onvifParam->nProfile; i++)
	{
		/*onvif profile config*/
		sprintf(tmpBuf,"onvif_profile_name_%d",i);
		//sprintf(tmpBuf,"onvif_profile_name");
		strcpy(onvifParam->Profile[i].profileName,tmpBuf);
		sprintf(tmpBuf,"onvif_profile_token_%d",i);
		strcpy(onvifParam->Profile[i].profileToken,tmpBuf);
		/*video source config*/
		sprintf(tmpBuf,"vscd_name_%d",i);
		strcpy(onvifParam->Profile[i].VSCD.Vname,tmpBuf);
		onvifParam->Profile[i].VSCD.Vcount = 1;//test
		sprintf(tmpBuf,"vscd_token_%d",i);
		strcpy(onvifParam->Profile[i].VSCD.Vtoken,tmpBuf);
		sprintf(tmpBuf,"vscd_source_token_%d",i);
		strcpy(onvifParam->Profile[i].VSCD.VsourceToken,tmpBuf);
		onvifParam->Profile[i].VSCD.windowx = 1;
		onvifParam->Profile[i].VSCD.windowy = 1;	
		/*video encoder config*/
		sprintf(tmpBuf,"vecd_name_%d",i);
		strcpy(onvifParam->Profile[i].VECD.Vename,tmpBuf);
		sprintf(tmpBuf,"vecd_token_%d",i);
		strcpy(onvifParam->Profile[i].VECD.Vetoken,tmpBuf);
		onvifParam->Profile[i].VECD.VeuserCount = 1;
		onvifParam->Profile[i].VECD.sessiontimeout = 720000;
		/*test----*/
		onvifParam->Profile[i].VSCD.windowWidth = 1280;
		onvifParam->Profile[i].VSCD.windowHeight = 720;
		onvifParam->Profile[i].VECD.Rwidth = 1280;
		onvifParam->Profile[i].VECD.Rheight = 720;
		onvifParam->Profile[i].VECD.frameratelimit = 30;
		onvifParam->Profile[i].VECD.encodinginterval = 3;
		onvifParam->Profile[i].VECD.Vequality = 10;
		onvifParam->Profile[i].VECD.videoMulticastConfig.nIPType = 0;
		onvifParam->Profile[i].VECD.videoMulticastConfig.port = 553;
		onvifParam->Profile[i].VECD.videoMulticastConfig.ttl = 60;
		onvifParam->Profile[i].VECD.videoMulticastConfig.autostart = 1;
		onvifParam->Profile[i].VECD.bitratelimit = 500;
		onvifParam->Profile[i].VECD.H264govlength = 30;
		onvifParam->Profile[i].VECD.H264profile = 1;
		onvifParam->Profile[i].VECD.Mpeggovlength = 30;
		onvifParam->Profile[i].VECD.Mpegprofile = 1;
		strcpy(onvifParam->Profile[i].VECD.videoMulticastConfig.IPv4Addr,"192.168.2.86");
	}
	/*device info config*/
	strcpy(onvifParam->DeviceInfo.hostName,"NDC300L-H");
	strcpy(onvifParam->DeviceInfo.manufacturer,"maxfort");
	strcpy(onvifParam->DeviceInfo.Model,"NDC300L-H");
	strcpy(onvifParam->DeviceInfo.Hardware,"NDC300L-H");//test
	strcpy(onvifParam->DeviceInfo.Firmware,"NDC300L-H");
	strcpy(onvifParam->DeviceInfo.DeviceID,"NDC300L-H");//test
	/*PTZ config*/
	strcpy(onvifParam->PtzProfile.Pname,"ptz_profile_name");
	strcpy(onvifParam->PtzProfile.Ptoken,"ptz_profile_token");
	strcpy(onvifParam->PtzProfile.PnodeToken,"ptz_profile_pnodetoken");
	/*image config*/
	onvifParam->Image.Brightness = 128;
	onvifParam->Image.ColorSaturation = 128;
	onvifParam->Image.Contrast = 128;
	onvifParam->Image.BacklightCompenSationMod = 0;
	onvifParam->Image.Sharpness = 128;
	onvifParam->Image.WideDynamicMode = 0;
	onvifParam->Image.WhiteBalanceMode = 0;
	onvifParam->Image.BackLightLevel = 20;
	onvifParam->Image.CrGain = 0;
	onvifParam->Image.CbGain = 0;
	/*test---------*/
	onvifConfig.Profile[0].VECD.Vencoder = 2;
	strcpy(onvifConfig.Profile[0].VECD.rtsp,"rtsp://192.168.2.86:554/stream1");
	/*onvifConfig.Profile[1].VECD.Vencoder = 2;
	strcpy(onvifConfig.Profile[1].VECD.rtsp,"rtsp://172.16.3.97:554/5.264");
	onvifConfig.Profile[2].VECD.Vencoder = 0;
	strcpy(onvifConfig.Profile[2].VECD.rtsp,"rtsp://172.16.3.97:554");
	onvifConfig.Profile[3].VECD.Vencoder = 2;
	strcpy(onvifConfig.Profile[3].VECD.rtsp,"rtsp://172.16.3.97:554/5.264");*/
	strcpy(onvifParam->NTP.ntpServer,"172.16.3.97");
	onvifParam->NTP.currentTime = 100;
	onvifParam->DHCP.DhcpFlag = 0;
	strcpy(onvifParam->NET.IP,"192.168.2.86");
	strcpy(onvifParam->NET.Mac,"000c29034cff");
	strcpy(onvifParam->NET.GateWay,"192.168.2.1");
	strcpy(onvifParam->NET.Dns,"192.168.0.1");
	onvifParam->NetProtoPort.httpPort = 80;
	onvifParam->NetProtoPort.httpsPort = 81;
	onvifParam->NetProtoPort.rtspPort1 = 554;
	onvifParam->NetProtoPort.rtspPort2 = 555;
	onvifParam->NetProtoPort.rtspPort3 = 556;
	onvifParam->NetProtoPort.rtspPort4 = 557;
	
}
